import { EventTarget, native, sys } from "cc";
import { LogMgr } from "../components/LogMgr";
import { BaseNativeDto } from "./nativeDataModels/BaseNativeDto";
import { SettingDataMgr } from "../managers/SettingDataMgr";
export enum NativeBridgeType {
    // 分发
    getNetworkStatus = 'getNetworkInfo',
    reqDeviceInfo = 'getDeviceInfoFromNative',
    reqDeviceId = 'getDeviceIdFromNative',
    getChannel = 'getAndroidChannel',
    useSystemAlbum = 'jumpToAlbum',
    jumpToStore = 'JumpToAppStore',
    jumpPermissionSettings = 'jumpToPermissionPage',
    vibrate = 'vibrateMobile',
    addReportTags = 'addReportTags',
    shareToFacebookRequest = 'shareToFb',
    shareBySystemRequest = 'shareBySys',
    shareToMessengerRequest = 'shareToMsger',
    getAppVersionRequest = 'getNativeVersion',
    getApnsToken = 'getApnsToken',
    signOutFromGg = 'signOutFromGg',
    signInFromGg = 'signInFromGg',
    signInFromFb = 'signInFromFb',
    signOutFromFb = 'signOutFromFb',
    signInFromApple = 'signInFromApple',
    getNotificatonStatus = 'getNotificationSwitch',
    gotoNotificationSettings = 'jumpToNotification',
    setPaymentVerifyUrl = 'setPaymentVerifyUrl',
    payByNative = 'payByNative',
    handleUnconsumedPurchase = 'handleUnconsumedPurchase',
    showLoading = 'showLoading',
    dismissLoading = 'dismissLoading',
    // 回调
    getNetworkStatusResult = 'getNetworkInfoResult',
    getDeviceInfoResult = 'getDeviceInformationResult',
    getDeviceIdResult = 'getDeviceIdFromNativeResult',
    getChannelResult = 'getChannelInfoResult',
    pickImageResult = 'getImageResult',
    shareToFacebookResponse = 'shareToFbResult',
    getAppVersionResponse = 'getNativeVersionResult',
    apnsTokenUpdated = 'onApnsTokenUpdated',
    getNotificatonStatusResult = 'getNotificationSwitchResult',
    onCompleteNativePayment = 'onCompleteNativePayment'
}
export class NativeBridgeMgr {
    private static _instance:NativeBridgeMgr;
    private _eventTarget:EventTarget = new EventTarget();
    public static get instance():NativeBridgeMgr{
        if(this._instance ==null){
            this._instance = new NativeBridgeMgr();
        }
        return this._instance;
    }
    public init():void{
        this.initEvent();
    }
      /**监听原生那边发过来的信息*/
    public on(type:NativeBridgeType, callback: any, thisArg?: any, once?: boolean): void {
        this._eventTarget.on(type, callback, thisArg, once);
    }
    public off(type:NativeBridgeType, callback: any, thisArg?: any,): void {
        this._eventTarget.off(type, callback, thisArg);
    }
    public offAll(thisArg: any): void {
        this._eventTarget.removeAll(thisArg);
    }
    public getInitData():void{
    }
    /**震动*/
    public startVibrate(s?: string) {
        if (SettingDataMgr.instance.vibrationEnabled) {
            if (sys.isNative) {
                LogMgr.log('已发送震动事件');
                native.jsbBridgeWrapper.dispatchEventToNative(NativeBridgeType.vibrate, s);
            }
        }
    }
    private onResponseData(type:string,data:string):void{
       LogMgr.log("请求native返回>>"+type+"<<数据>>"+data);
        let nativeData:any = JSON.parse(data);
        this.emit(type,nativeData);
    }
    private initEvent():void{
      
    }
    private emit(type: string, data:BaseNativeDto): void {
        this._eventTarget.emit(type, data);
    }
}